package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.FindAppItem1Bean;
import com.zuimei.zuimei.utils.ImgUtil;
import com.zuimei.zuimei.utils.ScreenUtil;

import java.util.List;

/**
 * Created by Jogger on 2017/1/17.
 * 发现页recycleview条目适配器
 */

public class Item1RecycleViewItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Object> mAppList;
    private Context mContext;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(View view, FindAppItem1Bean.AppsBean appBean);
    }

    public Item1RecycleViewItemAdapter(Context context) {
        mContext = context;
    }

    public void loadData(List<Object> appList) {
        mAppList = appList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mAppList.get(position) instanceof String) {
            return 0;
            //标签布局
        } else if (mAppList.get(position) instanceof FindAppItem1Bean.AppsBean) {
            //应用布局
            return 1;
        }
        return -1;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.rv_item1_item_title, parent,
                    false);
            return new MyTitleViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.rv_item1_item, parent,
                    false);
            return new MyContentViewHolder(view);
        }

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Object o = mAppList.get(position);
        if (holder instanceof MyTitleViewHolder) {
            MyTitleViewHolder titleViewHolder = (MyTitleViewHolder) holder;
            titleViewHolder.tv_label.setText((String) o);
        } else if (holder instanceof MyContentViewHolder) {
            MyContentViewHolder contentViewHolder = (MyContentViewHolder) holder;
            final FindAppItem1Bean.AppsBean appBean = (FindAppItem1Bean.AppsBean) o;
            Picasso.with(mContext)
                    .load(appBean.getIcon_image())
                    .config(Bitmap.Config.RGB_565)
                    .into(contentViewHolder.iv_head);
            contentViewHolder.tv_title.setText((appBean).getTitle());
            contentViewHolder.tv_sub_title.setText((appBean).getSub_title());
            Picasso.with(mContext)
                    .load((appBean).getCover_image())
                    .config(Bitmap.Config.RGB_565)
                    .transform(ImgUtil.getTransformation(ScreenUtil.getSrceenWidth(mContext)))
                    .into(contentViewHolder.iv_center);
            String tags = "";
            for (int j = 0; j < (appBean).getTags().size(); j++) {
                tags += "#" + appBean.getTags().get(j);
            }
            contentViewHolder.tv_tags.setText("# 每日最美 " + tags);
            contentViewHolder.tv_digest.setText((appBean).getDigest());
            contentViewHolder.tv_up_times.setText(appBean.getUp_times() + "");
            contentViewHolder.ll_content_container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onItemClick(view, appBean);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mAppList.size();
    }

    class MyContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        ImageView iv_head;
        TextView tv_sub_title;
        ImageView iv_center;
        TextView tv_digest;
        TextView tv_tags;
        TextView tv_up_times;
        LinearLayout ll_content_container;

        MyContentViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_sub_title = (TextView) itemView.findViewById(R.id.tv_sub_title);
            iv_center = (ImageView) itemView.findViewById(R.id.iv_center);
            tv_digest = (TextView) itemView.findViewById(R.id.tv_digest);
            tv_tags = (TextView) itemView.findViewById(R.id.tv_tags);
            tv_up_times = (TextView) itemView.findViewById(R.id.tv_up_times);
            ll_content_container = (LinearLayout) itemView.findViewById(R.id.ll_content_container);
        }
    }

    class MyTitleViewHolder extends RecyclerView.ViewHolder {
        TextView tv_label;

        MyTitleViewHolder(View itemView) {
            super(itemView);
            tv_label = (TextView) itemView.findViewById(R.id.tv_label);
        }
    }
}
